<template>
    <el-dialog
        :visible.sync="dialogVisible"
        :title="'添加【'+formData.specName+'】规格'"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="400px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px" label-position="top">
            <el-form-item label="规格值名称" prop="specValue">
                <el-input v-model="formData.specValue"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {SpecApi} from '@/api/mall/spec'

export default {
    name: 'AddOneSpecValue',
    components: {},
    data() {
        return {
            formData: {
                specName: '',
                specId: '',
                specValue: ''
            },
            rules: {
                specValue: [
                    {required: true, message: '请输入规格值', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false
        }
    },
    methods: {
        // 显示组件
        showBox(data) {
            this.formData.specId = data.specId
            this.formData.specName = data.specName

            if (this.$refs['formRef'] !== undefined) {
                this.$refs['formRef'].resetFields()
            }
            this.dialogVisible = true
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    SpecApi.addOneSpecValue(that.formData).then((specValueId) => {
                        that.dialogVisible = false
                        that.$emit('refresh',specValueId)
                    })
                }
            })
        }

    }
}
</script>
